package cn.yunyichina.reservation.controller.console;

import cn.yunyichina.provider.framework.dubbo.service.ReservationService;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.reservation.common.ResponseEnum;
import cn.yunyichina.reservation.common.jackson.Jackson;
import cn.yunyichina.reservation.constant.HttpResponse;
import cn.yunyichina.reservation.constant.Interface;
import cn.yunyichina.reservation.vo.ConsoleUserVo;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * Created by Lullaby on 2016/8/24.
 */
@RestController
@RequestMapping("console")
public class ConsoleUserController {

    private static final String RESERVATION_CONSOLE_USER_LIST = "yyt.reservation.console.user.list";

    private static final String[] PRIVATE_ACCOUNTS = {"admin", "dev"};

    @RequestMapping("users")
    public HttpResponse getConsoleUsers() {
        HttpResponse httpResponse = new HttpResponse();
        Response serviceResponse = ReservationService.callHumpFromInternal(
                RESERVATION_CONSOLE_USER_LIST, null
        );
        if (serviceResponse == null) {
            return httpResponse.failure(ResponseEnum.FAILURE.getCode(), ResponseEnum.FAILURE.getMessage());
        }
        if (Interface.SUCCESS.equals(serviceResponse.getResultCode())) {
            String result = serviceResponse.getResult();
            List<LinkedHashMap> users = Jackson.getBeanJsonData(result, List.class);
            if (!CollectionUtils.isEmpty(users)) {
                List<String> list = new ArrayList<>();
                for (LinkedHashMap map : users) {
                    ConsoleUserVo vo = Jackson.getBeanJsonData(Jackson.getBaseJsonData(map), ConsoleUserVo.class);
                    boolean isAdd = true;
                    for (String str : PRIVATE_ACCOUNTS) {
                        if (str.equals(vo.getUsername())) {
                            isAdd = false;
                            break;
                        }
                    }
                    if (isAdd) {
                        list.add(vo.getUsername());
                    }
                }
                httpResponse.setData(list);
            }
        } else {
            httpResponse.failure(serviceResponse.getResultCode(), serviceResponse.getResultMessage());
        }
        return httpResponse;
    }

}
